package com.oauth.global.client.ui;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.HorizontalFlowPanel;
import com.oauth.global.client.beans.AppInfoBean;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;

public class StatusUpdate extends Composite {

	private static StatusUpdateUiBinder uiBinder = GWT.create(StatusUpdateUiBinder.class);

	interface StatusUpdateUiBinder extends UiBinder<DecoratedStackPanel, StatusUpdate> {
	}

	@UiField
	Button updateButton;

	@UiField
	DecoratedStackPanel stackPanel;

	@UiField
	VerticalPanel statusUpdatePanel;

	@UiField
	FlowPanel imagePanel;

	@UiField
	TextArea textArea;

	@UiField
	DeckPanel deckPanel;

	private CheckBox[] checkBoxes;

	private GlobalRemoteServiceAsync mGlobalRemoteServiceAsync;

	private String applicationName = "Status update";

	public StatusUpdate() {
		initWidget(uiBinder.createAndBindUi(this));
		stackPanel.setStackText(0, "<div class=\"stackText\">" + applicationName + "</div>", true);
	}

	public void setService(GlobalRemoteServiceAsync pGlobalRemoteServiceAsync) {
		mGlobalRemoteServiceAsync = pGlobalRemoteServiceAsync;
	}

	public void initImages() {
		imagePanel.clear();
		deckPanel.showWidget(1);
		mGlobalRemoteServiceAsync
				.getApplicationImagesForStatus(new OAuthAsnyCallBack<List<AppInfoBean>>() {

					@Override
					public void onSuccess(List<AppInfoBean> pApplicationList) {
						if (pApplicationList == null || pApplicationList.isEmpty()) {
							statusUpdatePanel
									.add(new HTML(
											"There are no application which features status update.<br/>Click application link in top right corner to add more application's"));
						} else {
							int size = pApplicationList.size();
							checkBoxes = new CheckBox[size];
							for (AppInfoBean url : pApplicationList) {
								HorizontalFlowPanel element = new HorizontalFlowPanel();
								Image img = new Image(url.getImageUrl());
								img.setTitle(url.getName());
								img.setSize("15px", "15px");
								element.add(img);
								Label label = new Label(url.getName());

								element.add(label);

								checkBoxes[--size] = new CheckBox(element.getElement()
										.getInnerHTML(), true);
								checkBoxes[size].setFormValue("" + url.getId());

								imagePanel.add(checkBoxes[size]);
							}
							deckPanel.showWidget(0);
						}
					}
				});
	}

	@UiHandler("updateButton")
	void onClick(ClickEvent e) {
		updateButton.setEnabled(false);
		updateButton.setText("Updating...");
		deckPanel.showWidget(1);
		Set<Long> applicationIdSet = new HashSet<Long>();

		for (int i = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].getValue()) {
				applicationIdSet.add(Long.parseLong(checkBoxes[i].getFormValue()));
			}
		}

		mGlobalRemoteServiceAsync.updateStatus(textArea.getText(), applicationIdSet,
				new OAuthAsnyCallBack<Boolean>() {

					@Override
					public void onSuccess(Boolean pArg0) {
						deckPanel.showWidget(2);
					}
				});
		textArea.setText("");
		updateButton.setEnabled(true);
		updateButton.setText("Update");
	}
}
